package com.vq.util;

/**
 * <h1>文本转义器</h1>
 */
public class Escape {
    /**
     * 截取不包括头尾的字符串
     * @param str
     * @param begin
     * @param end
     * @return
     */
    static String subStringInner(String str, String begin, String end) {
        int a = str.indexOf(begin);
        int b = str.indexOf(end);
        String s = str.substring(a+begin.length(), b);
        return s;
    }
    /**
     * 截取包括头尾的字符串
     * @param str
     * @param begin
     * @param end
     * @return
     */
    static String subStringOuter(String str, String begin, String end) {
        int a = str.indexOf(begin);
        int b = str.indexOf(end);
        String s = str.substring(a, b+end.length());
        return s;
    }
    /**
     * 将带有a标签的信息转换为功能性标签（需在全部转换为显示性标签后使用）
     * @param str
     * @return
     */
    static String subA(String str) {
        String begin = "&lt;a&gt;";
        String end = "&lt;/a&gt;";
        String a = subStringOuter(str, begin, end);
        String b = subStringInner(str, begin, end);
        StringBuilder s = new StringBuilder();
        s.append("<button style=\"width: 100%;margin-left: 0%;margin-top: 1%;font-family: inherit;font-size: 13px;" +
                "                    background: #409eff;color: white;padding: 0.7em 1em;padding-left: 0.9em;" +
                "                    display: flex;align-items: center;border: none;border-radius: 10px;transition: all 0.2s;\" " +
                "              onclick=\"window.open('"+b+"')\">" +
                "        <svg style=\"display: block;transform-origin: center center;transition: transform 0.3s ease-in-out;\" " +
                "             height=\"24\" width=\"24\" viewBox=\"0 0 24 24\" xmlns=\"http://www.w3.org/2000/svg\">" +
                "          <path d=\"M0 0h24v24H0z\" fill=\"none\"></path>" +
                "          <path d=\"M1.946 9.315c-.522-.174-.527-.455.01-.634l19.087-6.362c.529-.176.832.12.684.638l-5.454 19.086c-.15.529-.455.547-.679.045L12 14l6-8-8 6-8.054-2.685z\" fill=\"currentColor\"></path>" +
                "        </svg>" +
                "        <span style=\"display: block;margin-left: 0.3em;transition: all 0.3s ease-in-out;\">链接："+b+"</span>" +
                "      </button>");
//        s.append("<button style=\"width: 100%; margin-left: 0%; margin-top: 1%;display: flex;" +
//                "      align-items: center;" +
//                "      font-family: inherit;" +
//                "      font-weight: 500;" +
//                "      font-size: 17px;" +
//                "      padding: 0.8em 1.5em 0.8em 1.2em;" +
//                "      color: white;" +
//                "      background: #ad5389;" +
//                "      background: linear-gradient(0deg, rgba(77,54,208,1) 0%, rgba(132,116,254,1) 100%);" +
//                "      border: none;" +
//                "      box-shadow: 0 0.7em 1.5em -0.5em #4d36d0be;" +
//                "      letter-spacing: 0.05em;" +
//                "      border-radius: 20em;\" onclick=\"window.open('"+b+"')\">" +
//                "        <svg style=\"margin-right: 8px;\" xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\" width=\"24\" height=\"24\"><path fill=\"none\" d=\"M0 0h24v24H0z\"></path><path fill=\"currentColor\" d=\"M1 14.5a6.496 6.496 0 0 1 3.064-5.519 8.001 8.001 0 0 1 15.872 0 6.5 6.5 0 0 1-2.936 12L7 21c-3.356-.274-6-3.078-6-6.5zm15.848 4.487a4.5 4.5 0 0 0 2.03-8.309l-.807-.503-.12-.942a6.001 6.001 0 0 0-11.903 0l-.12.942-.805.503a4.5 4.5 0 0 0 2.029 8.309l.173.013h9.35l.173-.013zM13 12h3l-4 5-4-5h3V8h2v4z\"></path></svg>" +
//                "        <span>"+b+"</span>" +
//                "      </button>");
        return str.replaceAll(a, s.toString());
    }
    /**
     * 将显示性标签转换为功能性标签
     * @param str
     * @return
     */
    static String convertHtml(String str) {
        str=str.replaceAll("&","&amp;");
        str=str.replaceAll("<","&lt;");
        str=str.replaceAll(">","&gt;");
        str=str.replaceAll("\"","&quot;");
        str=str.replaceAll(" ","&emsp;");

        return str;
    }
    /**
     * 将功能性标签转换为显示性标签
     * @param str
     * @return
     */
    static String convertText(String str) {
        str=str.replaceAll("\n","<p></p>");

        return str;
    }
    /**
     * 将内容中特殊字符转换为显示性字符以及从转义字符全部转换为具有HTML标签格式的内容
     * @param str
     * @return
     */
    public static String getHTML(String str) {
        str=convertHtml(str);
        while (str.contains("&lt;a&gt;")&&str.contains("&lt;/a&gt;")) {
            str=subA(str);
        }
        str=convertText(str);

        return str;
    }
}
